package cn.com.guage.dtm.metadata.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.guage.dtm.common.annotation.Log;
import cn.com.guage.dtm.common.enums.BusinessType;
import cn.com.guage.dtm.metadata.domain.MdSys;
import cn.com.guage.dtm.metadata.domain.MdTable;
import cn.com.guage.dtm.metadata.service.IMdModuleService;
import cn.com.guage.dtm.metadata.service.IMdSysService;
import cn.com.guage.dtm.metadata.service.IMdTableService;
import cn.com.guage.dtm.common.core.controller.BaseController;
import cn.com.guage.dtm.common.core.domain.AjaxResult;
import cn.com.guage.dtm.common.utils.poi.ExcelUtil;
import cn.com.guage.dtm.common.core.page.TableDataInfo;

/**
 * MD定义Controller
 * 
 * @author liujun
 * @date 2021-06-28
 */
@Controller
@RequestMapping("/metadata/table")
public class MdTableController extends BaseController
{
    private String prefix = "metadata/table";

    @Autowired
    private IMdTableService mdTableService;
    
    @Autowired
    private IMdModuleService mdModuleService;
    
    @Autowired
    private IMdSysService mdSysService;

    @RequiresPermissions("metadata:table:view")
    @GetMapping()
    public String table(ModelMap mmap)
    {
    	MdSys mdSys=new MdSys();
    	List<MdSys> mdSysList = mdSysService.selectMdSysList(mdSys);
    	mmap.put("mdSysList", mdSysList);
        return prefix + "/table";
    }

    /**
     * 查询MD定义列表
     */
    @RequiresPermissions("metadata:table:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MdTable mdTable)
    {
        startPage();
        List<MdTable> list = mdTableService.selectMdTableList(mdTable);
        return getDataTable(list);
    }

    /**
     * 导出MD定义列表
     */
    @RequiresPermissions("metadata:table:export")
    @Log(title = "MD定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MdTable mdTable)
    {
        List<MdTable> list = mdTableService.selectMdTableList(mdTable);
        ExcelUtil<MdTable> util = new ExcelUtil<MdTable>(MdTable.class);
        return util.exportExcel(list, "MD定义数据");
    }

    /**
     * 新增MD定义
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
    	MdSys mdSys=new MdSys();
    	List<MdSys> mdSysList = mdSysService.selectMdSysList(mdSys);
        mmap.put("mdSysList", mdSysList);
        return prefix + "/add";
    }

    /**
     * 新增保存MD定义
     */
    @RequiresPermissions("metadata:table:add")
    @Log(title = "MD定义", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MdTable mdTable)
    {
        return toAjax(mdTableService.insertMdTable(mdTable));
    }

    /**
     * 修改MD定义
     */
    @GetMapping("/edit/{tableId}")
    public String edit(@PathVariable("tableId") Long tableId, ModelMap mmap)
    {
        MdTable mdTable = mdTableService.selectMdTableById(tableId);
        mmap.put("mdTable", mdTable);
        return prefix + "/edit";
    }

    /**
     * 修改保存MD定义
     */
    @RequiresPermissions("metadata:table:edit")
    @Log(title = "MD定义", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MdTable mdTable)
    {
        return toAjax(mdTableService.updateMdTable(mdTable));
    }

    /**
     * 删除MD定义
     */
    @RequiresPermissions("metadata:table:remove")
    @Log(title = "MD定义", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mdTableService.deleteMdTableByIds(ids));
    }
}
